{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/njuciairs/wangshuai/test/FinancialNagetiveEntityJudge'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "from evaluation.evaluate import evaluate\n",
    "from data_utils.basic_data import load_train_val_dataset,load_basic_dataset\n",
    "from results_process.regulizer import remove_nine,remove_short_entity\n",
    "from results_process.utils import load_model_rs\n",
    "from results_process.bert_entity_model import reduce_rs_by_id\n",
    "from functools import reduce\n",
    "import numpy as np\n",
    "from data_utils.bert_multi_class_data import get_train_val_data_loader, get_test_loader,TestEntityDataset\n",
    "import pandas as pd\n",
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_df = load_model_rs(model_name='multi_class_cross1',version_id=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfs = [load_model_rs(model_name='multi_class_cross1',version_id=i) for i in range(1,10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_df = load_basic_dataset(split='test')\n",
    "test_dataset = TestEntityDataset(test_df, max_len=400)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "texts = [sample.text for sample in test_dataset]\n",
    "entity = [t.split('[SEP]')[0][5:] for t in texts]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "for df in dfs:\n",
    "    df['key_entity'] = entity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "group = pd.concat(dfs).groupby(['id','key_entity'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_list = []\n",
    "for (id,key),df in group:\n",
    "    label = Counter(df['predict_labels']).most_common(1)[0][0]\n",
    "    rs_list.append((id,key,label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_df = pd.DataFrame(rs_list,columns=['id','key_entity','predict_label'])[['id','predict_label','key_entity']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# raw_df['key_entity'] = entity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_map = {}\n",
    "for id,label,entity in raw_df.values:\n",
    "    if id not in rs_map:\n",
    "        rs_map[id] = ([label],[entity])\n",
    "    else:\n",
    "        rs_map[id][0].append(label)\n",
    "        rs_map[id][1].append(entity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "items = []\n",
    "for k,v in rs_map.items():\n",
    "    labels,entities = v\n",
    "    senti = int(np.mean(labels) >= 1)\n",
    "    keys = []\n",
    "    for l,e in zip(labels,entities):\n",
    "        if l==2:\n",
    "            keys.append(e)\n",
    "    key_entity = ';'.join(keys)\n",
    "    if len(keys)==0 or senti==0:\n",
    "        key_entity = np.nan\n",
    "    items.append((k,senti,key_entity))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>negative</th>\n",
       "      <th>key_entity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>00049297</td>\n",
       "      <td>1</td>\n",
       "      <td>小资钱包;资易贷;资易贷金融信息服务有限公司</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>000b8b75</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0012d20a</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0033ebe3</td>\n",
       "      <td>1</td>\n",
       "      <td>联璧金融</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>003b1540</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4995</td>\n",
       "      <td>ffa46c98</td>\n",
       "      <td>1</td>\n",
       "      <td>小资钱包;资易贷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4996</td>\n",
       "      <td>ffc0005d</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4997</td>\n",
       "      <td>ffd1497a</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4998</td>\n",
       "      <td>fff09e68</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4999</td>\n",
       "      <td>fffe28dd</td>\n",
       "      <td>1</td>\n",
       "      <td>黑火金融</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            id  negative              key_entity\n",
       "0     00049297         1  小资钱包;资易贷;资易贷金融信息服务有限公司\n",
       "1     000b8b75         0                     NaN\n",
       "2     0012d20a         0                     NaN\n",
       "3     0033ebe3         1                    联璧金融\n",
       "4     003b1540         0                     NaN\n",
       "...        ...       ...                     ...\n",
       "4995  ffa46c98         1                小资钱包;资易贷\n",
       "4996  ffc0005d         0                     NaN\n",
       "4997  ffd1497a         0                     NaN\n",
       "4998  fff09e68         0                     NaN\n",
       "4999  fffe28dd         1                    黑火金融\n",
       "\n",
       "[5000 rows x 3 columns]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_df = pd.DataFrame(items,columns=['id','negative','key_entity'])\n",
    "raw_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "#去重：把更短的去掉\n",
    "import numpy as np\n",
    "def remove_short_entity_by_long(entity_str):\n",
    "    \"\"\"\n",
    "    除去key_entity中同一实体的较短名称\n",
    "    :param entity_str:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    if not isinstance(entity_str, str):\n",
    "        return entity_str\n",
    "    entities = entity_str.split(';')\n",
    "    states = np.ones(len(entities))\n",
    "    for i, e in enumerate(entities):\n",
    "        for p in entities:\n",
    "            if e in p and len(e) < len(p):\n",
    "                print('removed %s by %s'%(e,p))\n",
    "                states[i] = 0\n",
    "    rs = []\n",
    "    for i, e in enumerate(entities):\n",
    "        if states[i] == 1:\n",
    "            rs.append(e)\n",
    "    rs = ';'.join(rs)\n",
    "    return rs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_trans_map():\n",
    "    from data_utils.basic_data import load_basic_dataset\n",
    "    train_df = load_basic_dataset('train')\n",
    "    srcs = train_df['entity'].map(lambda x :list(str(x).split(';')))\n",
    "    dests =  train_df['key_entity'].map(lambda x :list(str(x).split(';')))\n",
    "    trans_map = {}\n",
    "    for srcs,dests in list(zip(srcs,dests)):\n",
    "        for src in srcs:\n",
    "            if src == '':\n",
    "                continue\n",
    "            for e in srcs:\n",
    "                if e== '':\n",
    "                    continue\n",
    "                if (src in e or e in src) and e!=src:\n",
    "                    if src in dests:\n",
    "                        trans_map[src+'-'+e] = src\n",
    "                        trans_map[e+'-'+src] = src\n",
    "                    if e in dests:\n",
    "                        trans_map[src+'-'+e] = e\n",
    "                        trans_map[e+'-'+src] = e\n",
    "    return trans_map\n",
    "def trans_keys(trans_map,entity_str):\n",
    "    if not isinstance(entity_str,str):\n",
    "        return entity_str\n",
    "    es = list(filter(lambda x:str(x).strip()!='',entity_str.split(';')))\n",
    "    rs = set()\n",
    "    for e in es:\n",
    "        finded = False\n",
    "        for y in es:\n",
    "            if e+'-'+y in trans_map and e!=y:\n",
    "                rs.add(trans_map[e+'-'+y])\n",
    "                finded = True\n",
    "        if not finded:\n",
    "            rs.add(e)\n",
    "    if len(rs) > 0:\n",
    "        rs = ';'.join(list(rs))\n",
    "    else:\n",
    "        rs = np.nan\n",
    "    return rs\n",
    "trans_map = get_trans_map()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_df = raw_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "removed 米咖 by 米咖网\n",
      "removed 成都小额贷款 by 成都小额贷款公司\n",
      "removed 钱包 by 随手记钱包\n",
      "removed 随手记 by 随手记钱包\n",
      "removed 海钜信达 by 深圳市海钜信达投资发展有限公司\n",
      "removed 宜贷网 by ?宜贷网\n",
      "removed 天下投 by 深圳市富通天下投资管理有限公司\n",
      "removed 深圳市富通 by 深圳市富通天下投资管理有限公司\n",
      "removed 富通天下 by 深圳市富通天下投资管理有限公司\n",
      "removed 老虎理财 by 两只老虎理财\n",
      "removed 两只老虎 by 两只老虎理财\n",
      "removed 陆金所 by 西部陆金所\n",
      "removed 小额贷 by 恒隆小额贷款\n",
      "removed 小额贷 by 广州市恒隆小额贷款有限公司\n",
      "removed 小额贷 by 小额贷款有限公司\n",
      "removed 恒隆小额贷款 by 广州市恒隆小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 广州市恒隆小额贷款有限公司\n",
      "removed 宜贷网 by 宜贷网（原易贷网）\n",
      "removed 易贷网 by 宜贷网（原易贷网）\n",
      "removed 无忧借条 by ????无忧借条\n",
      "removed 嘉盛 by 嘉盛国际\n",
      "removed 雪橙 by 雪橙金服\n",
      "removed 智融财富 by 深圳市智融财富电商投资有限公司\n",
      "removed 高盛国际 by gs-forex高盛国际\n",
      "removed 山东海倍电子商务 by 山东海倍电子商务股份有限公司\n",
      "removed 国润 by 江西国润\n",
      "removed 随行付支付 by 随行付支付有限公司山西分公司\n",
      "removed 随行付 by 随行付支付\n",
      "removed 随行付 by 随行付支付有限公司山西分公司\n",
      "removed 粤融泰富 by 广东粤融泰富网络信息服务有限公司\n",
      "removed 天合 by 安徽天合联盟科技有限公司\n",
      "removed 天合 by 天合联盟\n",
      "removed 天合联盟 by 安徽天合联盟科技有限公司\n",
      "removed 北京华澳翼时代 by 北京华澳翼时代信息技术有限责任公司\n",
      "removed 北京华澳融信 by 北京华澳融信国际投资管理咨询有限公司\n",
      "removed 渤海创投 by 渤海创投集团通\n",
      "removed 云财富 by 外滩云财富\n",
      "removed 易贷金融 by  (北京)资易贷金融信息服务有限公司\n",
      "removed 易商通 by 北京易商通科技有限公司\n",
      "removed 红太阳 by 湖南红太阳电源新材料股份有限公司\n",
      "removed 深圳光彩 by 深圳光彩投资控股集团有限公司\n",
      "removed 壹佰金融 by 深圳壹佰金融\n",
      "removed 和耕传承基金 by 和耕传承基金销售有限公司控股\n",
      "removed 宜信 by 宜信普惠\n",
      "removed 宜信 by 宜信惠民\n",
      "removed 瑞波 by 瑞波币\n",
      "removed 滴水贷 by ????滴水贷\n",
      "removed 翱晟投资 by 台州翱晟投资公司\n",
      "removed 深圳高新 by 深圳高新盛\n",
      "removed 高新盛 by 深圳高新盛\n",
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 天天投 by 天天投金融\n",
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 华金融 by 高仕华金融\n",
      "removed 汇鑫小额贷款 by 汇鑫小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 汇鑫小额贷款有限公司\n",
      "removed 联璧 by 联璧金融\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 深圳高新 by 深圳高新盛创投电子商务有限公司\n",
      "removed 高新盛 by 深圳高新盛创投电子商务有限公司\n",
      "removed 齐鲁商品 by 齐鲁商品交易中心\n",
      "removed 亚太投资 by 北京亚太投资\n",
      "removed 汇聚财富 by 上海汇聚财富\n",
      "removed 渤海创投 by ?渤海创投子公司智慧蜂巢\n",
      "removed 小额贷 by 中合华惠农村小额贷款有限公司\n",
      "removed 小额贷 by 小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 中合华惠农村小额贷款有限公司\n",
      "removed 御顺金融 by 成都御顺金融贷款公司\n",
      "removed 盛付通 by 上海盛付通电子支付服务有限公司陕西分公司\n",
      "removed 星投资 by 泽星投资\n",
      "removed 亿金融 by 优亿金融\n",
      "removed 恒宇 by 恒宇天泽\n",
      "removed 圣盈信 by 圣盈信CIFS\n",
      "removed 零钱罐 by 零钱罐APP\n",
      "removed 小额贷 by 万源农村小额贷款有限公司与沈秋云\n",
      "removed 小额贷 by 小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 万源农村小额贷款有限公司与沈秋云\n",
      "removed 节节贷 by 广西弘尚节节贷集团(节节资本)\n",
      "removed 节节贷 by 广西弘尚节节贷集团\n",
      "removed 广西弘尚节节贷集团 by 广西弘尚节节贷集团(节节资本)\n",
      "removed 资易贷 by 资易贷北京金融信息服务有限公\n",
      "removed 点融 by 点融网\n",
      "removed 中南大宗 by 中南大宗商品电子商务有限公司\n",
      "removed 智云 by 智云金融\n",
      "removed 易贷金融 by  (北京)资易贷金融信息服务有限公司\n",
      "removed 玖富 by 玖富?投诉量最多\n",
      "removed fx by fx福克斯\n",
      "removed fx by onefx\n",
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 米融 by 易米融\n",
      "removed 中吴财富 by 上海中吴财富投资管理集团有限公司\n",
      "removed 节节贷 by 广西弘尚节节贷网络信息服务集团有限公司\n",
      "removed 兴业财富 by 北方兴业财富\n",
      "removed 贵金属 by 贵金属投资\n",
      "removed 沃德 by 沃德斯国际\n",
      "removed 蜜蜂 by 蜜蜂财富\n",
      "removed 中银 by 中银消费金融\n",
      "removed 中银 by 中银消费金融有限公司\n",
      "removed 中银消费金融 by 中银消费金融有限公司\n",
      "removed 钱宝 by 钱宝财\n",
      "removed 房融 by 房融所\n",
      "removed 随行付支付 by 随行付支付有限公司\n",
      "removed 随行付 by 随行付支付\n",
      "removed 随行付 by 随行付支付有限公司\n",
      "removed 资易贷 by 资易贷(北京）金融信息有限公司公司\n",
      "removed 冠群驰骋 by 冠群驰骋商务信息咨询（天津）有限公司\n",
      "removed 优宝 by 优宝汇\n",
      "removed 青岛九州商品交易中心 by 青岛九州商品交易中心有限公司\n",
      "removed 海贷 by 海贷金服体验金\n",
      "removed 海贷 by 海贷金服\n",
      "removed 海贷金服 by 海贷金服体验金\n",
      "removed 二元期权 by 金盛二元期权\n",
      "removed 钱宝 by 钱宝财\n",
      "removed 世纪贷 by 世纪贷互联网金融服务有限公司\n",
      "removed 海象理财 by 北京海象理财\n",
      "removed 银通投资 by 中润银通投资北京有限公司\n",
      "removed 华夏信财 by 华夏信财信息咨询（上海）有限公司芜湖分公司\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 仁和融兴 by 青岛仁和融兴投资有限公司\n",
      "removed 海象理财 by 北京海象理财\n",
      "removed 商银信支付 by 商银信支付服务有限公司\n",
      "removed 钱海湾 by 钱海湾金融公司\n",
      "removed 钱海湾 by 钱海湾金融\n",
      "removed 钱海湾金融 by 钱海湾金融公司\n",
      "removed 河北滨海大宗商品交易市场 by 河北滨海大宗商品交易市场服务有限公司\n",
      "removed 大宗商品交易 by 河北滨海大宗商品交易市场\n",
      "removed 大宗商品交易 by 河北滨海大宗商品交易市场服务有限公司\n",
      "removed 优库速购 by 深圳优库速购\n",
      "removed igofx by igofx平台\n",
      "removed 市大时代 by 深圳市大时代资产管理有限公司\n",
      "removed 时贷 by 大时贷\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 星投资 by 泽星投资\n",
      "removed 易贷金融 by  (北京)资易贷金融信息服务有限公司\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 信e贷 by 久信e贷\n",
      "removed 中赢投 by 中赢投资\n",
      "removed 玖财 by 玖财通\n",
      "removed 懒财主 by 前海懒财主金融\n",
      "removed 懒财主 by 海懒财主金融信息服务（深圳）有限公司\n",
      "removed 和信 by 资和信\n",
      "removed 宜贷网 by 宜贷网（原易贷网）\n",
      "removed 易贷网 by 宜贷网（原易贷网）\n",
      "removed 米融 by 易米融\n",
      "removed 日融财富 by 宁波日融财富投资管理有限公司\n",
      "removed 蜜蜂 by 蜜蜂财富\n",
      "removed 资易贷 by 资易贷（小资钱包）\n",
      "removed 小资钱包 by 资易贷（小资钱包）\n",
      "removed 资易贷 by 小资钱包公司(资易贷平台)\n",
      "removed 小资钱包 by 小资钱包公司(资易贷平台)\n",
      "removed 以太坊 by 以太坊ETH\n",
      "removed 上海易贷网 by 上海易贷网金融信息服务有限公司\n",
      "removed 上海文化产权交易所 by 上海文化产权交易所股份有限公司\n",
      "removed 中子星投资 by 中子星投资有限公司\n",
      "removed 星投资 by 中子星投资\n",
      "removed 星投资 by 中子星投资有限公司\n",
      "removed 36氪 by 36氪股权众筹\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 联金所 by 联金所康\n",
      "removed 金易融 by 金易融（北京）网络科技有限公司\n",
      "removed 钱宝 by 钱宝财\n",
      "removed 信和大金融 by 传信和大金融\n",
      "removed 宝银创赢 by 上海宝银创赢投资管理有限公司\n",
      "removed 创赢投资 by 上海宝银创赢投资管理有限公司\n",
      "removed 凯福德 by KFD凯福德\n",
      "removed 瑞财 by 恒瑞财富\n",
      "removed 中海投 by 上海中海投资产管理有限公司\n",
      "removed 中海投 by 中海投资\n",
      "removed 中海投资 by 上海中海投资产管理有限公司\n",
      "removed 易贷金融 by （北京）资易贷金融信息服务有限公司\n",
      "removed 唐小僧 by 唐小僧之后\n",
      "removed 北京华澳融信 by 北京华澳融信国际投资管理咨询有限公司\n",
      "removed 米融 by 易米融\n",
      "removed 长征财富 by 长征财富资产管理有限公司宝应支公司\n",
      "removed 老虎金融 by 老虎金融信息服务（北京）有限公司\n",
      "removed 冠群驰骋 by 冠群驰骋投资管理(北京)有限公司\n",
      "removed 冠群驰骋 by 冠群驰骋投资关联(北京)有限公司\n",
      "removed 小资钱包 by  资易贷(小资钱包）\n",
      "removed 小额贷 by 金久农村小额贷款有限公司\n",
      "removed 小额贷 by 小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 金久农村小额贷款有限公司\n",
      "removed 银网贷 by 超银网贷\n",
      "removed tbc by tbc（海湾资本）澳大利亚asic\n",
      "removed 翱晟投资 by 台州翱晟投资公司\n",
      "removed 聚财猫 by ????聚财猫\n",
      "removed 资易贷 by 北京资易贷公司（小资钱包)\n",
      "removed 小资钱包 by 北京资易贷公司（小资钱包)\n",
      "removed 仁和融兴 by 青岛仁和融兴投资有限公司\n",
      "removed 日融财富 by 宁波日融财富投资管理有限公司\n",
      "removed 钱吧 by 弹钱吧\n",
      "removed 冠e通 by 冠e通平台\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 一号家居网 by 一号家居网装饰公司\n",
      "removed 易商通 by 北京易商通科技有限公司\n",
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 山东海倍电子商务 by 山东海倍电子商务股份有限公司\n",
      "removed 简贷 by 易简贷\n",
      "removed 汇金方格 by 汇金方格（北京）投资管理有限公司\n",
      "removed 融通资产 by 北京圆融通资产管理有限公司\n",
      "removed 资易贷 by 海淀资易贷平台\n",
      "removed 惠金服 by 城惠金服\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 乐贷 by 网乐贷\n",
      "removed 易商通 by 北京易商通科技有限公司\n",
      "removed 誉东方投资管理 by 深圳市誉东方投资管理平台\n",
      "removed 东方投 by 深圳市誉东方投资管理平台\n",
      "removed 东方投 by 誉东方投资管理\n",
      "removed 理财帝 by 理财帝国\n",
      "removed 冠群驰骋 by ????冠群驰骋\n",
      "removed 大宗商品交易 by 河北滨海大宗商品交易市场\n",
      "removed 时贷 by 森昊好时贷\n",
      "removed 贷联盟 by 新贷联盟\n",
      "removed 云金融 by 景云金融\n",
      "removed 澳瑞克 by OracleFX澳瑞克\n",
      "removed 新余铭沃 by 新余铭沃投资管理中心\n",
      "removed 赛伯乐绿科 by 深圳赛伯乐绿科投资管理有限公司\n",
      "removed 重庆赛伯乐盈科 by 重庆赛伯乐盈科股权投资基金管理有限公司\n",
      "removed 中吴财富 by 上海中吴财富投资管理集团有限公司\n",
      "removed 资易贷 by 资易贷（北京）金融信息服务公司\n",
      "removed 汇聚财富 by 上海汇聚财富投资\n",
      "removed 联璧 by 联璧金融\n",
      "removed 渤海商品交易 by 天津渤海商品交易所\n",
      "removed 富民投资 by 富民投资网\n",
      "removed 银通投资 by 中润银通投资(北京)有限公司\n",
      "removed 余盆网 by 北京华澳融信（余盆网）\n",
      "removed 北京华澳融信 by 北京华澳融信（余盆网）\n",
      "removed 三农金服 by 深圳三农金服\n",
      "removed 资易贷 by 资易贷（北京）金融信息服务有限公司旗\n",
      "removed 钱包 by 钱包网\n",
      "removed 卡宝 by 卡宝典\n",
      "removed 玖富 by 北京玖富集团\n",
      "removed 小资 by 小资钱包\n",
      "removed 嘉盛 by 嘉盛国际\n",
      "removed 时贷 by 大时贷\n",
      "removed 钱宝 by 钱宝财\n",
      "removed 渤海商品交易所 by 天津渤海商品交易所\n",
      "removed 渤海商品交易 by 渤海商品交易所\n",
      "removed 渤海商品交易 by 天津渤海商品交易所\n",
      "removed 中银 by 中银消费金融\n",
      "removed 中银 by 中银消费金融有限公司\n",
      "removed 中银消费金融 by 中银消费金融有限公司\n",
      "removed 富民投资 by 富民投资网\n",
      "removed 宜贷网 by ?宜贷网\n"
     ]
    }
   ],
   "source": [
    "rs_df['key_entity'] = rs_df['key_entity'].map(lambda x: trans_keys(trans_map,x)).map(remove_short_entity_by_long)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "negative_nan = rs_df[rs_df['negative']==1]\n",
    "negative_nan = negative_nan[negative_nan['key_entity'].map(lambda x:isinstance(x,float))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>negative</th>\n",
       "      <th>key_entity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>146</td>\n",
       "      <td>077a7f27</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>266</td>\n",
       "      <td>0d21f2cb</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>461</td>\n",
       "      <td>174a8660</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>588</td>\n",
       "      <td>1d110b8c</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>724</td>\n",
       "      <td>23acf103</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>848</td>\n",
       "      <td>2aa57004</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>892</td>\n",
       "      <td>2d2c9223</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>985</td>\n",
       "      <td>334437be</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1769</td>\n",
       "      <td>5cbe0c83</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1830</td>\n",
       "      <td>60900ec3</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1915</td>\n",
       "      <td>647b7e3a</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2295</td>\n",
       "      <td>763c0846</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2511</td>\n",
       "      <td>801fa398</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2629</td>\n",
       "      <td>8675ad50</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2819</td>\n",
       "      <td>900f229a</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2918</td>\n",
       "      <td>95ce999b</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2934</td>\n",
       "      <td>97068a46</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3006</td>\n",
       "      <td>9ada02f2</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3252</td>\n",
       "      <td>a757818f</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3326</td>\n",
       "      <td>ab2ec010</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3850</td>\n",
       "      <td>c73d1272</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4023</td>\n",
       "      <td>d00443e9</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4396</td>\n",
       "      <td>e355afe7</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4526</td>\n",
       "      <td>e9abc6b0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4856</td>\n",
       "      <td>f93cbbc2</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            id  negative key_entity\n",
       "146   077a7f27         1        NaN\n",
       "266   0d21f2cb         1        NaN\n",
       "461   174a8660         1        NaN\n",
       "588   1d110b8c         1        NaN\n",
       "724   23acf103         1        NaN\n",
       "848   2aa57004         1        NaN\n",
       "892   2d2c9223         1        NaN\n",
       "985   334437be         1        NaN\n",
       "1769  5cbe0c83         1        NaN\n",
       "1830  60900ec3         1        NaN\n",
       "1915  647b7e3a         1        NaN\n",
       "2295  763c0846         1        NaN\n",
       "2511  801fa398         1        NaN\n",
       "2629  8675ad50         1        NaN\n",
       "2819  900f229a         1        NaN\n",
       "2918  95ce999b         1        NaN\n",
       "2934  97068a46         1        NaN\n",
       "3006  9ada02f2         1        NaN\n",
       "3252  a757818f         1        NaN\n",
       "3326  ab2ec010         1        NaN\n",
       "3850  c73d1272         1        NaN\n",
       "4023  d00443e9         1        NaN\n",
       "4396  e355afe7         1        NaN\n",
       "4526  e9abc6b0         1        NaN\n",
       "4856  f93cbbc2         1        NaN"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "negative_nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "auxilary_rs = load_model_rs(model_name='BertSentiEntity',version_id=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "#df.loc[df[‘column_name’].isin(some_values)]\n",
    "auxilary_rs = auxilary_rs.loc[auxilary_rs['id'].isin(negative_nan['id'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'id,negative,predict,entity_list'"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "','.join(auxilary_rs.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_negative_entity(x):\n",
    "    id,negative,predict,entity_list = x\n",
    "    predict = eval(predict)\n",
    "    entity_list = eval(entity_list)\n",
    "    keys = []\n",
    "    for senti,entity in zip(predict,entity_list):\n",
    "        if senti==1:\n",
    "            keys.append(entity)\n",
    "    if len(keys)==0:\n",
    "        return np.nan\n",
    "    else:\n",
    "\n",
    "        return remove_short_entity_by_long(remove_short_entity(';'.join(keys)))\n",
    "auxilary_rs['key_entity1'] = auxilary_rs.apply(lambda x: get_negative_entity(x),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "multi_choice_rs = pd.read_csv('evaluation/tmp/multi_choice_cross1-9_1024.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for id,negative,key_entity in auxilary_rs[['id','negative','key_entity1']].values:\n",
    "#     rs_df.loc[rs_df['id']==id,'key_entity'] = key_entity\n",
    "#     rs_df.loc[rs_df['id']==id,'negative'] = negative\n",
    "for id,negative,key_entity in auxilary_rs[['id','negative','key_entity1']].values:\n",
    "    rs_df.loc[rs_df['id']==id,'key_entity'] =  multi_choice_rs.loc[multi_choice_rs['id']==id,'key_entity'].values[0]\n",
    "    rs_df.loc[rs_df['id']==id,'negative'] = int(multi_choice_rs.loc[multi_choice_rs['id']==id,'negative'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>negative</th>\n",
       "      <th>key_entity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>146</td>\n",
       "      <td>077a7f27</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>266</td>\n",
       "      <td>0d21f2cb</td>\n",
       "      <td>1</td>\n",
       "      <td>新华财富</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>461</td>\n",
       "      <td>174a8660</td>\n",
       "      <td>1</td>\n",
       "      <td>QOS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>588</td>\n",
       "      <td>1d110b8c</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>724</td>\n",
       "      <td>23acf103</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>848</td>\n",
       "      <td>2aa57004</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>892</td>\n",
       "      <td>2d2c9223</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>985</td>\n",
       "      <td>334437be</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1769</td>\n",
       "      <td>5cbe0c83</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1830</td>\n",
       "      <td>60900ec3</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1915</td>\n",
       "      <td>647b7e3a</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2295</td>\n",
       "      <td>763c0846</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2511</td>\n",
       "      <td>801fa398</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2629</td>\n",
       "      <td>8675ad50</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2819</td>\n",
       "      <td>900f229a</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2918</td>\n",
       "      <td>95ce999b</td>\n",
       "      <td>1</td>\n",
       "      <td>ZJLT项目</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2934</td>\n",
       "      <td>97068a46</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3006</td>\n",
       "      <td>9ada02f2</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3252</td>\n",
       "      <td>a757818f</td>\n",
       "      <td>1</td>\n",
       "      <td>网贷之家</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3326</td>\n",
       "      <td>ab2ec010</td>\n",
       "      <td>1</td>\n",
       "      <td>融360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3850</td>\n",
       "      <td>c73d1272</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4023</td>\n",
       "      <td>d00443e9</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4396</td>\n",
       "      <td>e355afe7</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4526</td>\n",
       "      <td>e9abc6b0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4856</td>\n",
       "      <td>f93cbbc2</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            id  negative key_entity\n",
       "146   077a7f27         1        NaN\n",
       "266   0d21f2cb         1       新华财富\n",
       "461   174a8660         1        QOS\n",
       "588   1d110b8c         0        NaN\n",
       "724   23acf103         1        NaN\n",
       "848   2aa57004         1        NaN\n",
       "892   2d2c9223         0        NaN\n",
       "985   334437be         1        NaN\n",
       "1769  5cbe0c83         0        NaN\n",
       "1830  60900ec3         1        NaN\n",
       "1915  647b7e3a         1        NaN\n",
       "2295  763c0846         0        NaN\n",
       "2511  801fa398         1        NaN\n",
       "2629  8675ad50         0        NaN\n",
       "2819  900f229a         1        NaN\n",
       "2918  95ce999b         1     ZJLT项目\n",
       "2934  97068a46         0        NaN\n",
       "3006  9ada02f2         0        NaN\n",
       "3252  a757818f         1       网贷之家\n",
       "3326  ab2ec010         1       融360\n",
       "3850  c73d1272         0        NaN\n",
       "4023  d00443e9         0        NaN\n",
       "4396  e355afe7         0        NaN\n",
       "4526  e9abc6b0         0        NaN\n",
       "4856  f93cbbc2         1        NaN"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rs_df.loc[rs_df['id'].isin(auxilary_rs['id'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_df.to_csv('tmp/auxilary_to_remove_nan2.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_df.loc[rs_df['key_entity'].map(lambda x:isinstance(x,float)),'negative'] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_df.to_csv('tmp/auxilary_to_remove_nan_totally1.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
